//
//  LYFTabBar.m
//  LYFWB
//
//  Created by lyf on 16/7/19.
//  Copyright © 2016年 lyf. All rights reserved.
//

#import "LYFTabBar.h"

@interface LYFTabBar()

/** 添加发布按钮 */
@property (nonatomic, weak) UIButton *publicButton;

@end

@implementation LYFTabBar


#pragma mark - 添加发布按钮并初始化 和 按钮方法;
/*通过代码init创建的
 会内部调用下面的两个方法
 */
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpPublicButton];
        
    }
    return self;
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder
{

    if (self = [super initWithCoder:aDecoder]) {
        [self setUpPublicButton];
        
    }
    return self;
}


/**
 * 初始化,添加按钮
 */
- (void)setUpPublicButton
{
    // 增加一个加号按钮
    UIButton *plusButton = [[UIButton alloc] init];
    [plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    [plusButton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:plusButton];
    self.publicButton = plusButton;
}
/* 发布按钮点击 */
-(void)plusClick
{
    LYFLog(@"点击了发布按钮");
}

#pragma mark - 设置按钮的尺寸和位置
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    /*设置每一个按钮的宽,高*/
    
    CGFloat buttonW = self.width / 5;
    CGFloat buttonH = self.height;
    
    // 按钮索引
    int buttonIndex = 0;
    
    // UITabBarButton类型
    //    Class tabBarButtonClass = NSClassFromString(@"UITabBarButton");
    
    // 设置所有UITabBarButton的frame
    for (UIView *child in self.subviews) {
        // 找到UITabBarButton
        if ([child isKindOfClass:[UIControl class]] && ![child isKindOfClass:[UIButton class]]) {
            //        if([child isKindOfClass:tabBarButtonClass]) {
            CGFloat buttonX = buttonW * buttonIndex;
            child.frame = CGRectMake(buttonX, 0, buttonW, buttonH);
            
            // 增加索引
            buttonIndex++;
            if (buttonIndex == 2) {
                buttonIndex++;
            }
        }
    }
    
    /** 设置加号按钮的位置和尺寸 */
    self.publicButton.size = self.publicButton.currentBackgroundImage.size;
    self.publicButton.center = CGPointMake(self.width * 0.5, self.height * 0.5);

}

@end
